<?php
/**
 * @file
 */

/**
 * Add, Edit Agnecy form
 */
function ffs_entity_form($form, &$form_state, $entity = NULL) {
  global $user;

  if (!isset($form_state['agency'])) {
    if (!isset($entity->name)) {
      $entity = new StdClass();
      $entity->name = NULL;
    }
    agency_object_prepare($entity);
    $form_state['agency'] = $entity;
  }
  else {
    $entity = $form_state['agency'];
  }

  $form['#attributes']['class'][] = 'ffs-agency-edit-form';
  $form['#agency'] = $entity;

  // Start with the default user account fields.
  // Account information.
  $form['agency'] = array(
    '#type'   => 'container',
    '#weight' => -10,
  );
  // Only show name field on registration form or user can change own username.
  $form['agency']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Agency Name'),
    '#required' => TRUE,
    '#default_value' => !empty($entity->name) ? $entity->name : '',
  );
  $form['agency']['tel'] = array(
    '#type' => 'textfield',
    '#title' => t('Telephone'),
    '#default_value' => !empty($entity->tel) ? $entity->tel : '',
  );
  $form['agency']['fax'] = array(
    '#type' => 'textfield',
    '#title' => t('Fax'),
    '#default_value' => !empty($entity->fax) ? $entity->fax : '',
  );
  $form['agency']['hangding_fee'] = array(
    '#type' => 'textfield',
    '#title' => t('Hangding Fee'),
    '#default_value' => !empty($entity->hangding_fee) ? $entity->hangding_fee : '0',
  );
  $form['agency']['status'] = array(
    '#type' => 'textfield',
    '#title' => t('Status'),
    '#required' => TRUE,
    '#default_value' => !empty($entity->status) ? $entity->status : 1,
  );
  $form['agency']['remark'] = array(
    '#type' => 'textarea',
    '#title' => t('Remark'),
    '#default_value' => !empty($entity->remark) ? $entity->remark : '',
  );

  // Attach field widgets, and hide the ones where the 'user_register_form'
  // setting is not on.
  $langcode = entity_language('agency', $form['#agency']);
  field_attach_form('agency', $form['#agency'], $form, $form_state, $langcode);

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  $form['#validate'][] = 'agency_form_edit_validate';
  // Add the final user registration form submit handler.
  $form['#submit'][] = 'agency_form_edit_submit';


  return $form;
}

/**
 * Validation function for the client form.
 */
function ffs_entity_form_edit_validate($form, &$form_state) {
  entity_form_field_validate('agency', $form, $form_state);
}

/**
 * Submit handler for the client form.
 */
function ffs_entity_form_edit_submit($form, &$form_state) {
  // Remove unneeded values.
  form_state_values_clean($form_state);

  $entity = $form['#agency'];

  entity_form_submit_build_entity('agency', $entity, $form, $form_state);

  // Populate $edit with the properties of $account, which have been edited on
  // this form by taking over all values, which appear in the form values too.
  $edit = array_intersect_key((array) $entity, $form_state['values']);
  $entity = agency_save($entity);

  $form_state['agency'] = $entity;
  drupal_set_message(t('This agency has been saved successful!'));
  drupal_goto('ffs/agency');
}